{% extends 'base.html' %}
{% load static %}

{% block title %}首页 - 教务信息管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 class="mb-1">
                    <i class="fas fa-tachometer-alt me-2 text-primary"></i>
                    仪表板
                </h2>
                <p class="text-muted mb-0">
                    欢迎回来，{{ user.first_name|default:user.username }}！
                    <span class="badge bg-primary ms-2">{{ user.userprofile.get_role_display }}</span>
                    {% if user.userprofile.college_name %}
                    <span class="badge bg-secondary ms-1">{{ user.userprofile.college_name }}</span>
                    {% endif %}
                </p>
            </div>
            <div class="text-muted">
                <small>
                    <i class="fas fa-clock me-1"></i>
                    {{ "now"|date:"Y年m月d日 H:i" }}
                </small>
            </div>
        </div>
    </div>
</div>

{% if user.userprofile.is_admin %}
<!-- 管理员视图 -->
<div class="row mb-4">
    <!-- 统计卡片 -->
    <div class="col-md-3 mb-3">
        <div class="card stats-card">
            <div class="card-body text-center">
                <div class="stats-number">{{ total_projects }}</div>
                <div class="stats-label">
                    <i class="fas fa-project-diagram me-1"></i>
                    项目总数
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3 mb-3">
        <div class="card stats-card">
            <div class="card-body text-center">
                <div class="stats-number">{{ total_subprojects }}</div>
                <div class="stats-label">
                    <i class="fas fa-tasks me-1"></i>
                    子项目总数
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3 mb-3">
        <div class="card stats-card">
            <div class="card-body text-center">
                <div class="stats-number">{{ total_users }}</div>
                <div class="stats-label">
                    <i class="fas fa-users me-1"></i>
                    用户总数
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3 mb-3">
        <div class="card stats-card">
            <div class="card-body text-center">
                <div class="stats-number">
                    {% for stat in subproject_status_stats %}
                        {% if stat.status == 'completed' %}{{ stat.count }}{% endif %}
                    {% empty %}0{% endfor %}
                </div>
                <div class="stats-label">
                    <i class="fas fa-check-circle me-1"></i>
                    已完成项目
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <!-- 最近项目 -->
    <div class="col-md-6 mb-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-project-diagram me-2"></i>
                    最近项目
                </h5>
            </div>
            <div class="card-body">
                {% if recent_projects %}
                <div class="list-group list-group-flush">
                    {% for project in recent_projects %}
                    <div class="list-group-item border-0 px-0">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <h6 class="mb-1">{{ project.name }}</h6>
                                <p class="mb-1 text-muted small">{{ project.description|truncatechars:50 }}</p>
                                <small class="text-muted">
                                    <i class="fas fa-calendar me-1"></i>{{ project.semester }}
                                    <i class="fas fa-user ms-2 me-1"></i>{{ project.admin_manager.first_name|default:project.admin_manager.username }}
                                </small>
                            </div>
                            <span class="badge bg-primary">{{ project.budget }}元</span>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                {% else %}
                <div class="empty-state">
                    <i class="fas fa-project-diagram"></i>
                    <p>暂无项目</p>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
    
    <!-- 最近子项目 -->
    <div class="col-md-6 mb-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-tasks me-2"></i>
                    最近子项目
                </h5>
            </div>
            <div class="card-body">
                {% if recent_subprojects %}
                <div class="list-group list-group-flush">
                    {% for subproject in recent_subprojects %}
                    <div class="list-group-item border-0 px-0">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <h6 class="mb-1">{{ subproject.name }}</h6>
                                <p class="mb-1 text-muted small">{{ subproject.project.name }}</p>
                                <small class="text-muted">
                                    <i class="fas fa-user me-1"></i>{{ subproject.manager }}
                                    <i class="fas fa-user-plus ms-2 me-1"></i>{{ subproject.created_by.first_name|default:subproject.created_by.username }}
                                </small>
                            </div>
                            <div class="text-end">
                                <span class="badge status-{{ subproject.status }} mb-1">
                                    {{ subproject.get_status_display }}
                                </span>
                                <br>
                                <small class="text-muted">{{ subproject.used_budget }}元</small>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                {% else %}
                <div class="empty-state">
                    <i class="fas fa-tasks"></i>
                    <p>暂无子项目</p>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>

{% else %}
<!-- 二级学院用户视图 -->
<div class="row mb-4">
    <!-- 我的统计 -->
    <div class="col-md-4 mb-3">
        <div class="card stats-card">
            <div class="card-body text-center">
                <div class="stats-number">{{ my_subprojects_count }}</div>
                <div class="stats-label">
                    <i class="fas fa-tasks me-1"></i>
                    我的子项目
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-4 mb-3">
        <div class="card stats-card">
            <div class="card-body text-center">
                <div class="stats-number">{{ my_completed_subprojects }}</div>
                <div class="stats-label">
                    <i class="fas fa-check-circle me-1"></i>
                    已完成项目
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-4 mb-3">
        <div class="card stats-card">
            <div class="card-body text-center">
                <div class="stats-number">
                    {% if my_subprojects_count > 0 %}
                        {{ my_completed_subprojects|floatformat:0 }}/{{ my_subprojects_count }}
                    {% else %}
                        0%
                    {% endif %}
                </div>
                <div class="stats-label">
                    <i class="fas fa-chart-pie me-1"></i>
                    完成率
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <!-- 我的子项目 -->
    <div class="col-md-6 mb-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-tasks me-2"></i>
                    我的子项目
                </h5>
            </div>
            <div class="card-body">
                {% if my_recent_subprojects %}
                <div class="list-group list-group-flush">
                    {% for subproject in my_recent_subprojects %}
                    <div class="list-group-item border-0 px-0">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <h6 class="mb-1">{{ subproject.name }}</h6>
                                <p class="mb-1 text-muted small">{{ subproject.project.name }}</p>
                                <small class="text-muted">
                                    <i class="fas fa-calendar me-1"></i>{{ subproject.created_at|date:"m-d" }}
                                </small>
                            </div>
                            <div class="text-end">
                                <span class="badge status-{{ subproject.status }} mb-1">
                                    {{ subproject.get_status_display }}
                                </span>
                                <br>
                                <small class="text-muted">{{ subproject.used_budget }}元</small>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                {% else %}
                <div class="empty-state">
                    <i class="fas fa-tasks"></i>
                    <p>您还没有创建子项目</p>
                    <a href="#" class="btn btn-primary btn-sm">创建第一个子项目</a>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
    
    <!-- 可用项目 -->
    <div class="col-md-6 mb-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-project-diagram me-2"></i>
                    可用项目
                </h5>
            </div>
            <div class="card-body">
                {% if available_projects %}
                <div class="list-group list-group-flush">
                    {% for project in available_projects %}
                    <div class="list-group-item border-0 px-0">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <h6 class="mb-1">{{ project.name }}</h6>
                                <p class="mb-1 text-muted small">{{ project.description|truncatechars:40 }}</p>
                                <small class="text-muted">
                                    <i class="fas fa-calendar me-1"></i>{{ project.semester }}
                                </small>
                            </div>
                            <div class="text-end">
                                <span class="badge bg-success mb-1">{{ project.remaining_budget }}元</span>
                                <br>
                                <small class="text-muted">剩余经费</small>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                {% else %}
                <div class="empty-state">
                    <i class="fas fa-project-diagram"></i>
                    <p>暂无可用项目</p>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endif %}

<!-- 快速操作 -->
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-bolt me-2"></i>
                    快速操作
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    {% if user.userprofile.is_admin %}
                    <div class="col-md-3 mb-2">
                        <a href="{% url 'projects:project_create' %}" class="btn btn-outline-primary w-100">
                            <i class="fas fa-plus me-2"></i>创建项目
                        </a>
                    </div>
                    <div class="col-md-3 mb-2">
                        <a href="{% url 'accounts:user_create' %}" class="btn btn-outline-success w-100">
                            <i class="fas fa-user-plus me-2"></i>创建用户
                        </a>
                    </div>
                    <div class="col-md-3 mb-2">
                        <a href="{% url 'common:excel_management' %}" class="btn btn-outline-info w-100">
                            <i class="fas fa-upload me-2"></i>导入数据
                        </a>
                    </div>
                    <div class="col-md-3 mb-2">
                        <a href="{% url 'accounts:user_list' %}" class="btn btn-outline-secondary w-100">
                            <i class="fas fa-users me-2"></i>用户管理
                        </a>
                    </div>
                    {% else %}
                    <div class="col-md-4 mb-2">
                        <a href="{% url 'subprojects:subproject_create' %}" class="btn btn-outline-primary w-100">
                            <i class="fas fa-plus me-2"></i>创建子项目
                        </a>
                    </div>
                    <div class="col-md-4 mb-2">
                        <a href="{% url 'subprojects:subproject_list' %}" class="btn btn-outline-info w-100">
                            <i class="fas fa-list me-2"></i>我的子项目
                        </a>
                    </div>
                    <div class="col-md-4 mb-2">
                        <a href="{% url 'accounts:profile' %}" class="btn btn-outline-secondary w-100">
                            <i class="fas fa-user me-2"></i>个人信息
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}